package iagovsothello.gui;

import iagovsothello.core.*;

import java.awt.*;

import javax.swing.JButton;

public class Square extends JButton {

	public static Color borderColor = Color.BLACK;
	public static Color player1Color = Color.WHITE;
	public static Color player2Color = Color.BLACK;
	public static Color backgroundColor = new Color(0,192,0);
	public static Color legalMovePlayer1 = new Color(0,160,0);
	public static Color legalMovePlayer2 = new Color(0,160,0);
	public static Color markColor = Color.RED;
	public static Color transparenceColor = new Color(128,128,128);
	
	private static final long serialVersionUID = 8946756354119341758L;
	
	private Position position;
	
	private boolean mark;
	
	/*
	 * 0: quadrato vuoto;
	 * 1: quadrato con pedina giocatore 1;
	 * 2: quadrato con pedina giocatore 2;
	 * 3: quadrato vuoto, mossa legale per giocatore 1;
	 * 4: quadrato vuoto, mossa legale per giocatore 2;
	 * altri valori: quadrato inesistente
	 * */
	private int status;

	public Square()
	{
		super();
		this.status = 0;
		this.mark = false;
	}
	
	public Square(Position position)
	{
		super();
		this.position = position;
		this.status = 0;
		this.mark = false;
	}
	
	public Square(Position position, int status) 
	{
		super();
		this.position = position;
		this.status = status;
		this.mark = false;
	}
	
	public int getStatus() 
	{
		return status;
	}

	public void setStatus(int status)
	{
		this.status = status;
	}

	public Position getPosition() 
	{
		return position;
	}
	
	public boolean isMarked()
	{
		return mark;
	}

	public void setMark(boolean mark) 
	{
		this.mark = mark;
	}

	public void paint(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		switch (status)
		{
		case 0:
			paintBG(g2,backgroundColor);
			paintBorder(g2,borderColor);
			break;
		case 1:
			paintBG(g2,backgroundColor);
			paintBorder(g2,borderColor);
			paintPiece(g2,player1Color);
			if (mark)
				paintMark(g2,markColor);
			break;
		case 2:
			paintBG(g2,backgroundColor);
			paintBorder(g2,borderColor);
			paintPiece(g2,player2Color);
			if (mark)
				paintMark(g2,markColor);
			break;
		case 3:
			paintBG(g2,backgroundColor);
			paintBorder(g2,borderColor);
			paintPiece(g2,legalMovePlayer1);
			break;
		case 4:
			paintBG(g2,backgroundColor);
			paintBorder(g2,borderColor);
			paintPiece(g2,legalMovePlayer2);
			break;
		default:
			paintBG(g2,transparenceColor);
			break;
		}
	}
	
	private void paintBG(Graphics2D g2, Color c)
	{
		g2.setColor(c);
		g2.fillRect(0, 0, getWidth(), getHeight());
	}
	
	private void paintBorder(Graphics2D g2, Color c)
	{
		g2.setColor(c);
		g2.drawRect(0, 0, getWidth()-1, getHeight()-1);
	}
	
	private void paintPiece(Graphics2D g2, Color c)
	{
		g2.setColor(c);
		g2.fillOval(5, 5, getWidth()-10, getHeight()-10);
	}
	
	private void paintMark(Graphics2D g2, Color c)
	{
		g2.setColor(c);
		g2.drawOval(5, 5, getWidth()-10, getHeight()-10);
		g2.drawOval(4, 4, getWidth()-8, getHeight()-8);
	}
}
